package com.yqjr.config;

import java.util.concurrent.Executor;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * SchedulingConfigurer： 将线程池注入TaskScheduler，并与ScheduledTaskRegistrar 关联；
 * AsyncConfigurer：用于异步处理
 * Created by wudl on 2017年10月20日
 */
@Configuration
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer,AsyncConfigurer{

	/**
	 * 异步任务
	 */
	@Override
	public Executor getAsyncExecutor() {
		Executor executor = taskScheduler();
		return executor;
	}

	/**
	 * 异步任务 异常处理
	 */
	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return new SimpleAsyncUncaughtExceptionHandler();
	}

	/**
	 * 并行任务
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		TaskScheduler taskScheduler = taskScheduler();
		taskRegistrar.setTaskScheduler(taskScheduler);
	}
	
	/**
	* 并行任务使用策略：多线程处理
	* 
	* @return ThreadPoolTaskScheduler 线程池
	*/
	@Bean(destroyMethod = "shutdown")
	public ThreadPoolTaskScheduler taskScheduler()
	{
	ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
	scheduler.setPoolSize(20);
	scheduler.setThreadNamePrefix("task-");
	scheduler.setAwaitTerminationSeconds(60);
	scheduler.setWaitForTasksToCompleteOnShutdown(true);
	return scheduler;
	}
	
}
